/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#include "StringUtils.h"
#include "tinyxml.h"

namespace gel2de {
  
   //-----------------------------------------------------------------------------
   
   bool startsWith(const std::string& s, const std::string& prefix)
   {
      return s.length() >= prefix.length()
         && s.compare(0, prefix.length(), prefix) == 0;
   }

   bool endsWith(const std::string& s, const std::string& suffix)
   {
      return s.length() >= suffix.length()
         && s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0;
   }

   std::string toLowerCase(const std::string& s)
   {
      std::string result(s.length(), ' ');
      for (int i = 0; i < s.length(); i++)
      {
         result[i] = tolower(s[i]);
      }
      return result;
   }

   std::string toUpperCase(const std::string& s)
   {
      std::string result(s.length(), ' ');
      for (int i = 0; i < s.length(); i++)
      {
         result[i] = toupper(s[i]);
      }
      return result;
   }

   bool stringToBool(const std::string& s)
   {
      return s.length() == 4
         && toupper(s[0]) == 'T'
         && toupper(s[1]) == 'R'
         && toupper(s[2]) == 'U'
         && toupper(s[3]) == 'E';
   }

   std::string trim(const std::string& s)
   {
      int iBegin = 0;
      while (iBegin < s.length() && isspace(s[iBegin])) iBegin++;
      if (iBegin == s.length()) return "";
      int iEnd = s.length();
      while (isspace(s[iEnd - 1])) iEnd--;
      return s.substr(iBegin, iEnd - iBegin);
   }

   std::vector<std::string> split(const std::string& s,
                                  const std::string& separator, int limit)
   {
      std::vector<std::string> result;
      if (s.length() == 0) return result;

      int i = 0;
      for (int n = 1; i < s.length() && n < limit; n++)
      {
         int j = s.find(separator, i);
         if (j == std::string::npos) break;
         result.push_back(s.substr(i, j - i));
         i = j + separator.length();
      }
      result.push_back(i < s.length() ? s.substr(i) : "");

      return result;
   }
  
} // gel2de::
